package com.lge.clock.timer;

import android.content.Context;
import android.os.PowerManager;
import android.util.Log;

public class TimerWakeLock {
	private static PowerManager.WakeLock sScreenWakeLock = null;
    private static PowerManager.WakeLock sCpuWakeLock = null;
    private static final String TAG	= "TimerWakeLock"; 
    
    /**
     * Method acquireCpuWakeLock.
     * @param context Context
     */
    public static void acquireCpuWakeLock(Context context) {
        Log.v(TAG, "Acquiring cpu wake lock");
        
        if (sCpuWakeLock != null) {
            return;
        }

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

        sCpuWakeLock = pm.newWakeLock(
                PowerManager.PARTIAL_WAKE_LOCK |
                PowerManager.ACQUIRE_CAUSES_WAKEUP |
                PowerManager.ON_AFTER_RELEASE, "ALStart");
        
        Log.d(TAG, "ACQUIRE_CAUSES_WAKEUP is set in TimerWakeLock.java acquireCpuWakeLock");
        
        sCpuWakeLock.acquire();
	}

    /**
     * Method acquireScreenWakeLock.
     * @param context Context
     */
    public static void acquireScreenWakeLock(Context context) {
    	Log.v(TAG, "Acquiring screen wake lock");
    	
        if (sScreenWakeLock != null) {
            return;
        }

        PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);

		sScreenWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK
				| PowerManager.ACQUIRE_CAUSES_WAKEUP
//				| PowerManager.ON_AFTER_RELEASE
				, "ALStart");
        
//        Log.d(TAG, "ACQUIRE_CAUSES_WAKEUP is set in TimerWakeLock.java acquireScreenWakeLock");
        
        sScreenWakeLock.acquire();
	}

    /**
     * Method release.
     */
    public static void release() {
    	Log.v(TAG, "Releasing wake lock");
    	
        if (sCpuWakeLock != null) {
        	Log.v(TAG, "Releasing cpu Wake lock");
            sCpuWakeLock.release();
            sCpuWakeLock = null;
        }
        if (sScreenWakeLock != null) {
        	Log.v(TAG, "Releasing screen Wake lock");
            sScreenWakeLock.release();
            sScreenWakeLock = null;
        }
    }
}
